const {createArea, delArea, updateArea, getAllArea} = require('../service/area')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')
const convert = require('../utils/convert')

const getArea = async () => {
  let result = await getAllArea()
  result = Array.prototype.slice.call(result)

  return result ? new SuccessModel(convert(result)) : new ErrorModel(addRoleFailInfo)
}

const postArea = async ({name, parentId = 0, level = 0}) => {
  const result = await createArea({name, parentId, level})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const deleteArea = async (id) => {
  const result = await delArea(id)
  return result ? new SuccessModel() : new ErrorModel(addRoleFailInfo)
}

const patchArea = async ({name, id}) => {
  const result = await updateArea({name, id})
  return result ? new SuccessModel() : new ErrorModel(addRoleFailInfo)
}

module.exports = {
  getArea,
  postArea,
  deleteArea,
  patchArea
}
